#include "client_test.h"

client_test::client_test(QObject *parent)
    : QObject(parent)
    , m_client(parent)
{
}

client_test::~client_test(void)
{
}

void client_test::run()
{
    login();
    get_items();
}

void client_test::login()
{
    m_client.host() = QString("127.0.0.1");
    m_client.set_data_delegate(this);
    nr_error_t status = m_client.login("admin", "password", this);
    Q_ASSERT(status == NR_ERROR_NONE);
}

void client_test::get_items()
{
    
}

void client_test::on_error(nr_error_t error)
{
    qDebug() << "Reply Error : " << error;
}

void client_test::on_reply(nr_object &reply)
{
    switch(reply.type())
    {
    case NR_OBJECT_REPLY_LOGIN:
        {
            nr_reply_login& reply_login = reply.object_ref<nr_reply_login>();

            qDebug() << "Reply login, cookie : " << reply_login.cookie() << 
                "Session ID : " << m_client.session_id();
            qDebug() << "Starting query items";

            m_client.get_device_items(this);
        }
        break;
    case NR_OBJECT_REPLY_GET_ITEMS:
        {
            nr_reply_get_items& reply_get_items = reply.object_ref<nr_reply_get_items>();
            nr_device_item_server &item_server = reply_get_items.root_item();

            qDebug() << "Reply get items, name : " << item_server.name() << 
                "ID : " << item_server.id();
            
            QList<nr_device_id_t> device_id_list;
            device_id_list.push_back(nr_device_id_t("APMD7XX000001"));
            // device_id_list.push_back(nr_device_id_t("APMD7XX000011"));

            // test start data
            //m_client.start_data(device_id_list, this);

            //test query data
            QDateTime begin_time = QDateTime::fromString("2012-12-21 10:51:00","yyyy-MM-dd hh:mm:ss");
            QDateTime end_time   = QDateTime::fromString("2012-12-23 12:52:00","yyyy-MM-dd hh:mm:ss");
            m_client.query_data("APMD7XX000001",begin_time,end_time,this);
        }
        break;
    case NR_OBJECT_REPLY_START_DATA:
        {
            nr_reply_start_data& reply_start_data = reply.object_ref<nr_reply_start_data>();
            
            qDebug() << "Reply start data ";
        }
        break;
    case NR_OBJECT_REPLY_QUERY_DATA:
        {
            nr_reply_query_data& reply_query_data = reply.object_ref<nr_reply_query_data>();
            QList<nr_data*> data_list = reply_query_data.data_list();

            QListIterator<nr_data*> it(data_list);
            for ( int i=0; it.hasNext(); i++ )
            {
                nr_data* data = it.next();
                qDebug() << i+1 << " " << "test_query_data, ID : " << data->device_id();
                qDebug() << data->time().toString();
                for(QVariantHash::iterator it = data->args().begin();
                    it != data->args().end(); ++it)
                {
                    qDebug() << "Name : " <<  it.key() << "Value : " 
                        << it.value().toString();
                }
            }
        }
        break;
    }

}

nr_error_t client_test::on_receive_data(nr_data *data)
{
    Q_ASSERT(data);

    qDebug() << "Received data , ID : " << data->device_id();

    for(QVariantHash::iterator it = data->args().begin();
        it != data->args().end(); ++it)
    {
        qDebug() << "Name : " <<  it.key() << "Value : " 
            << it.value().toString();
    }

    return NR_ERROR_NONE;
}

nr_error_t client_test::on_receive_event(nr_event *evt)
{
    return NR_ERROR_NONE;
}
